<?php  
class ControllerModuleNews extends Controller {
	protected function index($setting) {
		$this->load->language('module/news');
		
		$this->load->model('news/article');
		$this->load->model('news/type');
		
		$this->load->model('tool/image');
		
		$this->data['text_read_older_post'] = $this->language->get('text_read_older_post');

		// Get all available types to display
		$news_module_types = $setting['types'];
		
		$news_types = $this->model_news_type->getNewsTypes();
		
		foreach ($news_module_types as $type){
			foreach ($news_types as $news_type){
				if($type == $news_type['type_id']) {
					$this->data['news_types'][] = $news_type;
					break;
				}
			}
			
			$data = array(
				'filter_type_id' => $type,
				'sort'  => 'na.date_added',
				'order' => 'DESC',
				'start' => 0,
				'limit' => $setting['limit'] + $setting['limit_next']
			);
			$news[$type] = $this->model_news_article->getArticles($data);
			
			$total_news_found = sizeof($news[$type]);
			
			$news_of_type = $news[$type]; 
			
			// Get highlight news 
			$this->data['big_news'][$type] = array();
			for ($i = 0; $i < $setting['limit']; $i++){
				if($i > ($total_news_found - 1)) {
					break;
				}
								
				if (!empty($news_of_type[$i]['image']) && file_exists(DIR_IMAGE . $news_of_type[$i]['image'])) {
					$thumb = $this->model_tool_image->resize($news_of_type[$i]['image'], 150, 150);
				} else {
					$thumb = $this->model_tool_image->resize('no_image.jpg', 150, 150);
				}
				
				$news = array(
					'article_id' => $news_of_type[$i]['article_id'],
					'title'		 => $news_of_type[$i]['title'],
					'header'	 => $news_of_type[$i]['header'],
					'thumb'		 => $thumb,
					'href'		 => $this->url->link('news/article', 'article_id=' . $news_of_type[$i]['article_id'])
				);
				$this->data['big_news'][$type][] = $news; 
			}
			
			// Get next news
			$this->data['small_news'][$type] = array();
			for ($i = $setting['limit']; $i < $setting['limit'] + $setting['limit_next']; $i++){
				if($i > ($total_news_found - 1)) {
					break;
				}
				
				if (!empty($news_of_type[$i]['image']) && file_exists(DIR_IMAGE . $news_of_type[$i]['image'])) {
					$thumb = $this->model_tool_image->resize($news_of_type[$i]['image'], 100, 100);
				} else {
					$thumb = $this->model_tool_image->resize('no_image.jpg', 100, 100);
				}
				
				$news = array(
					'article_id' => $news_of_type[$i]['article_id'],
					'title'		 => $news_of_type[$i]['title'],
					'thumb'		 => $thumb,
					'href'		 => $this->url->link('news/article', 'article_id=' . $news_of_type[$i]['article_id'])
				);
				$this->data['small_news'][$type][] = $news;
			}
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/news.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/news.tpl';
		} else {
			$this->template = 'default/template/module/news.tpl';
		}
		
		$this->render();
  	}
}
?>